package com.yunpeng.aidevice.cms.web.controllers.device;

import com.alibaba.fastjson.JSONObject;
import com.yunpeng.aidevice.biz.service.IDeviceBizService;
import com.yunpeng.aidevice.common.constant.ConfigConstantValues;
import com.yunpeng.common.dto.Result;
import com.yunpeng.aidevice.common.dto.cache.LoginUserCacheDTO;
import com.yunpeng.aidevice.common.dto.query.DeviceQueryDTO;
import com.yunpeng.aidevice.common.dto.save.DeviceSaveDTO;
import com.yunpeng.common.paging.PagingQueryDTO;
import com.yunpeng.common.util.ParametersCheckUtil;
import com.yunpeng.aidevice.common.vo.SimpleEntityVO;
import com.yunpeng.aidevice.common.vo.device.DeviceDetailVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * DeviceController
 *
 * @author : Moore(hemw)
 * @date : 2020-04-27
 */
@RestController
@RequestMapping("/api/device")
public class DeviceController {

    @Resource
    private IDeviceBizService deviceBizService;


    /**
     * 商户端的设备列表
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "list", method = RequestMethod.POST)
    public Result<PagingQueryDTO> list(@RequestBody DeviceQueryDTO deviceQueryDTO) {
        ParametersCheckUtil.isNotNull(deviceQueryDTO, "没有传递任何参数");
        ParametersCheckUtil.isNotNull(deviceQueryDTO.getPageSize(), "需要传入页数量");
        ParametersCheckUtil.isNotNull(deviceQueryDTO.getCurrentPage(), "需要传入页码");
        LoginUserCacheDTO loginUserCacheDTO = ConfigConstantValues.getLoginUserCacheDTO();
        deviceQueryDTO.setMerchantCode(loginUserCacheDTO.getMerchantCode());
        deviceQueryDTO.setTenantCode(loginUserCacheDTO.getTenantCode());
        return deviceBizService.queryDeviceList4Merchant(deviceQueryDTO);
    }

    /**
     * 新增设备
     *
     * @param deviceSaveDTO
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "addNew", method = RequestMethod.POST)
    public Result<Void> addNew(@RequestBody DeviceSaveDTO deviceSaveDTO) {
        ParametersCheckUtil.isNotNull(deviceSaveDTO, "没有传递任何参数");
        ParametersCheckUtil.isNotNull(deviceSaveDTO.getDeviceNumber(), "需要传入设备编号");
        ParametersCheckUtil.isNotNull(deviceSaveDTO.getDeviceName(), "需要传入设备名称");
        ParametersCheckUtil.collectionHasNotAllEmptyOrderEmpty(deviceSaveDTO.getOrgCodes(), deviceSaveDTO.getOrgNumbers(), "需要选择通行部门");
        ParametersCheckUtil.isNotNull(deviceSaveDTO.getDisabled(), "设备需要选择禁用或者启用");
        LoginUserCacheDTO loginUserCacheDTO = ConfigConstantValues.getLoginUserCacheDTO();
        deviceSaveDTO.setMerchantCode(loginUserCacheDTO.getMerchantCode());
        deviceSaveDTO.setTenantCode(loginUserCacheDTO.getTenantCode());
        return deviceBizService.addNewDevice(deviceSaveDTO, loginUserCacheDTO.getCode());
    }

    /**
     * 设备详情
     *
     * @param requestParam
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "detail", method = RequestMethod.POST)
    public Result<DeviceDetailVO> detail(@RequestBody JSONObject requestParam) {
        ParametersCheckUtil.mapIsNotNullOrderEmpty(requestParam, "没有传递任何参数");
        String deviceCode = requestParam.getString("deviceCode");
        ParametersCheckUtil.isNotBlank(deviceCode, "无效的设备代码");
        LoginUserCacheDTO loginUserCacheDTO = ConfigConstantValues.getLoginUserCacheDTO();
        return deviceBizService.deviceDetail(deviceCode, loginUserCacheDTO.getMerchantCode(), loginUserCacheDTO.getTenantCode());
    }

    /**
     * 修改设备
     *
     * @param deviceSaveDTO
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "modify", method = RequestMethod.POST)
    public Result<Void> modify(@RequestBody DeviceSaveDTO deviceSaveDTO) {
        ParametersCheckUtil.isNotNull(deviceSaveDTO, "没有传递任何参数");
        ParametersCheckUtil.isNotNull(deviceSaveDTO.getDeviceNumber(), "需要传入设备编号");
        ParametersCheckUtil.isNotNull(deviceSaveDTO.getDeviceName(), "需要传入设备名称");
        ParametersCheckUtil.collectionHasNotAllEmptyOrderEmpty(deviceSaveDTO.getOrgCodes(), deviceSaveDTO.getOrgNumbers(), "需要选择通行部门");
        ParametersCheckUtil.isNotNull(deviceSaveDTO.getDisabled(), "设备需要选择禁用或者启用");
        ParametersCheckUtil.isNotNull(deviceSaveDTO.getDeviceCode(), "需要传入设备代码");
        LoginUserCacheDTO loginUserCacheDTO = ConfigConstantValues.getLoginUserCacheDTO();
        deviceSaveDTO.setMerchantCode(loginUserCacheDTO.getMerchantCode());
        deviceSaveDTO.setTenantCode(loginUserCacheDTO.getTenantCode());
        return deviceBizService.updateDevice(deviceSaveDTO, loginUserCacheDTO.getCode());
    }

    /**
     * 启用设备
     *
     * @param requestParam
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "enable", method = RequestMethod.POST)
    public Result<Void> enable(@RequestBody JSONObject requestParam) {
        ParametersCheckUtil.mapIsNotNullOrderEmpty(requestParam, "没有传递任何参数");
        String deviceCode = requestParam.getString("deviceCode");
        ParametersCheckUtil.isNotBlank(deviceCode, "无效的设备代码");
        LoginUserCacheDTO loginUserCacheDTO = ConfigConstantValues.getLoginUserCacheDTO();
        return deviceBizService.enableDevice(deviceCode, loginUserCacheDTO.getCode(), loginUserCacheDTO.getMerchantCode(), loginUserCacheDTO.getTenantCode());
    }

    /**
     * 禁用设备
     *
     * @param requestParam
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "disable", method = RequestMethod.POST)
    public Result<Void> disable(@RequestBody JSONObject requestParam) {
        ParametersCheckUtil.mapIsNotNullOrderEmpty(requestParam, "没有传递任何参数");
        String deviceCode = requestParam.getString("deviceCode");
        ParametersCheckUtil.isNotBlank(deviceCode, "无效的设备代码");
        LoginUserCacheDTO loginUserCacheDTO = ConfigConstantValues.getLoginUserCacheDTO();
        return deviceBizService.disableDevice(deviceCode, loginUserCacheDTO.getCode(), loginUserCacheDTO.getMerchantCode(), loginUserCacheDTO.getTenantCode());
    }

    /**
     * 删除设备
     *
     * @param requestParam
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "remove", method = RequestMethod.POST)
    public Result<Void> remove(@RequestBody JSONObject requestParam) {
        ParametersCheckUtil.mapIsNotNullOrderEmpty(requestParam, "没有传递任何参数");
        String deviceCode = requestParam.getString("deviceCode");
        ParametersCheckUtil.isNotBlank(deviceCode, "无效的设备代码");
        LoginUserCacheDTO loginUserCacheDTO = ConfigConstantValues.getLoginUserCacheDTO();
        return deviceBizService.deleteDevice(deviceCode, loginUserCacheDTO.getMerchantCode(), loginUserCacheDTO.getTenantCode());
    }

    /**
     * 设备下拉菜单
     *
     * @param requestParam
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "dropDownList", method = RequestMethod.POST)
    public Result<List<SimpleEntityVO>> dropDownList(@RequestBody JSONObject requestParam) {
        ParametersCheckUtil.mapIsNotNullOrderEmpty(requestParam, "没有传递任何参数");
        String merchantCode = requestParam.getString("merchantCode");
        LoginUserCacheDTO loginUserCacheDTO = ConfigConstantValues.getLoginUserCacheDTO();
        if (StringUtils.isBlank(merchantCode)) {
            merchantCode = loginUserCacheDTO.getMerchantCode();
        }
        return deviceBizService.dropDownList(merchantCode, loginUserCacheDTO.getTenantCode());
    }

}
